//
//  WalletDetailController.swift
//  SCA
//
//  Created by tjun on 2025/9/6.
//

import UIKit

class WalletDetailController: BaseController {
    
    let coinModel: WalletCoinModel

    private lazy var header = WalletDetailHeaderView(frame: CGRectMake(0, 0, screenWidth, 258), coinModel: coinModel)
    private var datas: [TransactionRecordModel] = []

    private lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = AppStyle.color.background
        tableView.rowHeight = 54
        tableView.contentInsetAdjustmentBehavior = .never;
        tableView.register(TransactionRecordCell.self, forCellReuseIdentifier: TransactionRecordCell.identifier)
        tableView.estimatedRowHeight = 0
        tableView.estimatedSectionFooterHeight = 0
        tableView.estimatedSectionHeaderHeight = 0
        tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: tabBarSafeBottomMargin, right: 0)
        tableView.separatorStyle = .none
        return tableView
    }()
    
    init(coinModel: WalletCoinModel) {
        self.coinModel = coinModel
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.tableHeaderView = header
        tableView.addTo(view)
        tableView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(navBarHeight)
        }
        
        header.onActionButtonTapped = {[weak self] actionType in
            guard let self = self else { return }
            
            if actionType == .transfer {
                let transactionController = TransactionController(model: self.coinModel)
                navigationController?.pushViewController(transactionController, animated: true)
                return
            }
        }
        
        NotificationCenter.default.addObserver(self, selector: #selector(loadData), name: NotificationName.transactionSuccess, object: nil)
        loadData()
    }
    
    @objc func loadData() {
        WalletService.loadWalletDetail(coin: coinModel.coin) { models, totalAccount, total in
            self.header.totalValueLabel.text = "$\(formatAmount(totalAccount))"
            self.datas.append(contentsOf: models)
            self.tableView.reloadData()
        }
    }
    
 
}


extension WalletDetailController: UITableViewDataSource, UITableViewDelegate {
 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: TransactionRecordCell.identifier, for: indexPath) as! TransactionRecordCell
        cell.indexPath = indexPath
        let model = datas[indexPath.row]
        cell.model = model
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        
    }
    
}
